#include "U2RenderTexture.h"

#include "U2Exception.h"
#include "U2HardwarePixelBuffer.h"
#include "U2Image.h"


U2EG_NAMESPACE_USING


//-----------------------------------------------------------------------------
U2RenderTexture::U2RenderTexture(U2HardwarePixelBuffer *buffer, size_t zoffset):
	mBuffer(buffer), mZOffset(zoffset)
{
    mPriority = U2_REND_TO_TEX_RT_GROUP;
	mWidth = static_cast<unsigned int>(mBuffer->getWidth());
	mHeight = static_cast<unsigned int>(mBuffer->getHeight());
    mColourDepth = static_cast<unsigned int>(
		U2PixelUtil::getNumElemBits(mBuffer->getFormat()));
}
U2RenderTexture::~U2RenderTexture()
{
	mBuffer->_clearSliceRTT(0);
}

void U2RenderTexture::copyContentsToMemory(const U2PixelBox &dst, FrameBuffer buffer)
{
	if (buffer == FB_AUTO) buffer = FB_FRONT;
	if (buffer != FB_FRONT)
	{
		U2_EXCEPT(U2Exception::ERR_INVALIDPARAMS,
					"Invalid buffer.",
					"RenderTexture::copyContentsToMemory" );
	}

	mBuffer->blitToMemory(dst);
}
//---------------------------------------------------------------------
PixelFormat U2RenderTexture::suggestPixelFormat() const
{
	return mBuffer->getFormat();
}
//-----------------------------------------------------------------------------
U2MultiRenderTarget::U2MultiRenderTarget(const U2String &name)
{
    mPriority = U2_REND_TO_TEX_RT_GROUP;
	mName = name;
	/// Width and height is unknown with no targets attached
	mWidth = mHeight = 0;
}
//-----------------------------------------------------------------------------
void U2MultiRenderTarget::copyContentsToMemory(const U2PixelBox &dst, FrameBuffer buffer)
{
	U2_EXCEPT(U2Exception::ERR_INVALIDPARAMS, 
				"Cannot get MultiRenderTargets pixels",
				"MultiRenderTarget::copyContentsToMemory");
}